package com.sgb.kjwl.viewmodel.transport;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.CarInfo;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.repository.TransportRepertory;
import com.sgb.kjwl.view.ui.fragment.transport.GoodsFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GoodsModel extends BaseViewModel {

    private GoodsFragment goodsFragment;
    private TransportRepertory repertory;

    public GoodsModel(GoodsFragment fragment, TransportRepertory repertory){
        this.goodsFragment = fragment;
        this.repertory = repertory;
    }

    public void getGoodsType3(){
        NewAPIManager.getInstance().getLogisticsAPI().getAllGoodsType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;
                List<GoodsInfoTypeEntity3> carTypeList = response.body().getData() != null ? response.body().getData() : new ArrayList<>();
                if (response.body().getState().equals("ok")) {
                    GoodsInfoTypeEntity3 type = new GoodsInfoTypeEntity3("", "不限");
                    carTypeList.add(0, type);

                    if (goodsFragment != null) {
                        goodsFragment.getGoodsTypeData(carTypeList);
                    }
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.GOODS_INFO_TYPE, new Gson().toJson(carTypeList));
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }
    public void getCarInfo(){
        DialogHelper.showProgressDialog(goodsFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getCarInfo().enqueue(new Callback<BaseEntity<List<CarInfo>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarInfo>>> call, Response<BaseEntity<List<CarInfo>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    goodsFragment.showCarInfo(response.body().getData());
                } else {
                    MToast.showToast(goodsFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarInfo>>> call, Throwable t) {

            }
        });
    }
}
